package demo4.util;

import com.alibaba.druid.pool.DruidDataSource;
import demo4.pojo.DBConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Mikazuki on 2025/3/28.
 */
@Component
public class DBUtil implements IDBUtil {
    private static DruidDataSource dataSource;
    @Autowired
    private DBConfig config;
    private void init() {
        dataSource = new DruidDataSource();
        dataSource.setUrl(config.getUrl());
        dataSource.setUsername(config.getUsername());
        dataSource.setPassword(config.getPassword());
        dataSource.setInitialSize(3);
        dataSource.setMaxActive(10);
    }
    @Override
    public Connection getConnection() throws SQLException {
        if (dataSource == null) {
            init();
        }
        return dataSource.getConnection();
    }
}
